<template>
  <div class="container">
    <div class="left">
      <!-- 卡片视图区域 -->
      <el-card>
        <h3>轮播图管理</h3>
        <!-- 搜索与添加区域 -->
        <el-row :gutter="20">
          <el-col :span="6">
            <el-input placeholder="请输入搜索内容" v-model="queryInfo.value"
              @keyup.enter.native="getAllSwiper" clearable>
              <el-button slot="append" icon="el-icon-search"
                @click="getAllSwiper"></el-button>
            </el-input>
          </el-col>
        </el-row>

        <!-- 主体 -->
        <el-table :data="swiperList" style="width: 100%" border>
          <!-- id -->
          <el-table-column prop="_id" label="id">
          </el-table-column>
          <!-- 商品id -->
          <el-table-column prop="goods_id" label="商品id">
          </el-table-column>
          <!-- 图片 -->
          <el-table-column prop="image_src" label="头像">
            <template width="190" slot-scope="scope">
              <img style="width: 255px; height: 95px; border: none"
                :src="scope.row.image_src" v-show="!scope.row.isEdit" />
              <input type="file" @change="upImg" v-show="scope.row.isEdit" />
            </template>
          </el-table-column>
          <!-- 导航地址 -->
          <el-table-column prop="navigator_url" label="导航地址" width="200">
            <template width="90" slot-scope="scope">
              <el-input type="text" v-model="changeData.navigator_url"
                v-show="scope.row.isEdit" />
              <span
                v-show="!scope.row.isEdit">{{ scope.row.navigator_url }}</span>
            </template>
          </el-table-column>
          <!-- 联系方式 -->
          <el-table-column prop="open_type" label="打开方式" width="100">
          </el-table-column>
          <!-- 操作 -->
          <el-table-column prop="gender" label="操作" width="200">
            <template slot-scope="scope">
              <!-- 删除 -->
              <el-button size="mini" type="danger" icon="el-icon-delete"
                @click="deleteSwiper(scope.row._id)">删除</el-button>
              <!-- 修改 -->
              <el-button size="mini" type="primary" v-show="!scope.row.isEdit"
                icon="el-icon-edit" @click="change(scope.row._id)">
                修改
              </el-button>
              <!-- 保存 -->
              <el-button size="mini" type="success" v-show="scope.row.isEdit"
                @click="upChange"><i
                  class="el-icon-upload el-icon--right"></i>保存</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="pages">
          <el-pagination @size-change="" @current-change=""
            :current-page="queryInfo.currentPage" :page-sizes="[1, 2, 5, 10]"
            :page-size="queryInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapState: imgState, mapActions: imgAction } = createNamespacedHelpers("upimg");
import AdminAdd from './add/AdmintAdd.vue'
export default {
  components: { AdminAdd },
  data() {
    return {
      swiperList: [],
      changeData: {},
      changeNum: 0,
      queryInfo: { pageSize: 5, currentPage: 1, value: "" },
      total: 0,
    };
  },
  computed: {
    ...imgState(['imgsrc'])
  },
  methods: {
    ...imgAction(["upImg"]),
    // 获取轮播图
    async getAllSwiper() {
      // 发请求
      let { data } = await this.$api.bannerApi.getAllSwiper({
      });
      let arr = data.message
      arr.forEach((v) => {
        v.isEdit = false;
      });
      this.swiperList = arr
      console.log(data);
      // goods_id: "601fa4888820000078006717"
      // image_src: "http://web.woniulab.com:8005/images/3 (2)-1631608853273.jpg"
      // http://file.sooui.com/data/attachment/forum/202005/06/105759bffa88v1zw1aufff.jpg-view
      // navigator_url: "/pages/goods_detail/index?goods_id=38"
      // open_type: "navigate"
      // show: 1
      // state: 1
      // _id: "60dff4620f0f00003f0079c4"
    },
    //删除
    deleteSwiper(_id) {
      this.$api.bannerApi.deleteSwiper({ _id }).then(({ data }) => {
        console.log(data);
        if (data.code == 405) {
          this.$message.error(data.error);
          return
        }
        // 删除提示
        this.$message.success(data.message);
        this.getAllSwiper();

      });
    },
    // 修改
    change(id) {
      if (this.changeNum) return this.$message.error("请先完成当前修改");
      this.changeNum = 1;
      let res = this.swiperList.filter((v) => v._id == id)[0];
      res.isEdit = true;
      this.changeData = { ...res };
      console.log(this.changeData);
    },
    // 确认修改
    upChange() {
      this.$api.bannerApi
        .updateSwiper({ ...this.changeData, image_src: 'https://img2.baidu.com/it/u=2004014431,429829261&fm=26&fmt=auto' })
        .then(({ data }) => {
          console.log(data);
          this.$message.success(data.message);
          this.getAllSwiper();
          this.changeNum = 0;
        });
    },
  },
  created() {
    this.getAllSwiper();
  }
};
</script>

<style lang="scss" scoped>
img {
  width: 50px;
  height: 50px;
}

input {
  width: 100px;
}
.container {
  display: flex;
  .left {
    width: 100%;
  }
}
</style>
